//
//  MusicViewController.m
//  Feel_at_home
//
//  Created by 王静雨 on 15/9/29.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "MusicViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "SingleViewController.h"

@interface MusicViewController ()
@property (nonatomic ,strong) UIButton *playButton;
@property (nonatomic ,strong) UIImageView *platImage;
@property (nonatomic ,assign) BOOL y;
@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic ,strong) AVPlayerItem *playerItem;
@property (nonatomic ,strong) UIImageView *imageView1;
@property (nonatomic ,strong) UIImageView *imageView;
@property (nonatomic ,strong) NSTimer *timer;
@property (nonatomic ,strong) UIButton *buttonback;

@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.buttonback = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.buttonback setTitle:@"back" forState:UIControlStateNormal];
//    [self.buttonback setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
    self.buttonback.frame = CGRectMake(10, 80, 50, 50);
    [self.view addSubview:self.buttonback];
    [self.buttonback addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // Do any additional setup after loading the view.
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1420598946304.jpg"]];
    self.imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.imageView.backgroundColor = [UIColor colorWithRed:1.000 green:0.438 blue:0.502 alpha:1.000];
    self.imageView.alpha = 0.7;
    [self.view addSubview:self.imageView];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame =CGRectMake(160, 500, 50, 50);
    [self.view addSubview:self.playButton];
    
    self.imageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1420598946304.jpg"]];
    self.imageView1.frame = CGRectMake(50, 150, 280, 280);
    [self.view addSubview:self.imageView1];
    
    self.imageView1.layer.cornerRadius = 140;
    
    self.imageView1.layer.backgroundColor = [UIColor redColor].CGColor;
    self.imageView1.layer.borderWidth = 1;
    self.imageView1.layer.masksToBounds = YES;
    [self.imageView1 setBackgroundColor:[UIColor greenColor]];
    self.imageView1.tag = 10;
    
    
    if (self.y == NO ) {
        self.player = [[AVPlayer alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://hls.hz.qingting.fm/live/332.m3u8?bitrate=64&deviceid=bb43c9aa78c691a777fe2ae99424d4aa"];
        self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
        [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"] forState:UIControlStateNormal];
        
    }else
    {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"] forState:UIControlStateNormal];
    }
    [self.playButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)click:(UIButton*)button
{
//    SingleViewController *single = [[SingleViewController alloc] init];
//    [self.navigationController pushViewController:single animated:YES];
//    [self presentViewController:single animated:single completion:^{
    
//    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    
}

- (void)buttonClick:(UIButton *)button
{
    if (self.y == NO  || self.timer == nil) {
        [self.player play];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(xuanzhuang) userInfo:nil repeats:YES];
        
        self.y = YES;
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"] forState:UIControlStateNormal];
    }else
    {
        [self.player pause];
        self.y = NO;
        [self.timer invalidate];
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"] forState:UIControlStateNormal];
    }
}

- (void)xuanzhuang
{
    UIImageView *image1 = (UIImageView *)[self.view viewWithTag:10];
    image1.transform = CGAffineTransformRotate(image1.transform, -M_PI_4 * 1 /50);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
